package com.wnhz.work.yz.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.AddressBean;
import com.wnhz.work.yz.bean.BathroomDetailBean;
import com.wnhz.work.yz.utils.MyDialog;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;
import com.wnhz.work.yz.view.MyDatePicker2;
import com.wnhz.work.yz.view.MyDatePicker3;
import com.wnhz.work.yz.view.PickerView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者： zxb
 * 日期： 2016/12/21
 * 描述： 预约信息
 */
public class AppointMentMessageActivity extends BaseActivity implements View.OnClickListener {
    private static final int CODE_ADDRESS = 111;
    private View include_address, include_noAddress;
    private TextView tv_name, tv_phone, tv_address;
    private EditText et_square;
    private String addrId;

    private BathroomDetailBean bean;
    private TextView tv_appoint_time;
    private TextView tv_call_time;

    private String appointTime;
    private String callTime;
    private String square;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_appoint_ment_message);

        initData();
        initTitle();
        initView();
    }

    private TextView title;

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("预约信息");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
    }


    private void initData() {
        bean = (BathroomDetailBean) getIntent().getSerializableExtra("bean");
    }

    private void initView() {
        et_square = (EditText) findViewById(R.id.et_square);
        findViewById(R.id.rl_appoint_time).setOnClickListener(this);
        findViewById(R.id.rl_call_time).setOnClickListener(this);
        findViewById(R.id.tv_now).setOnClickListener(this);

        tv_appoint_time = (TextView) findViewById(R.id.tv_appoint_time);
        tv_call_time = (TextView) findViewById(R.id.tv_call_time);
        /**
         * 判断是否有地址选择显示隐藏的布局
         */
        include_address = findViewById(R.id.include_choose_address);//有地址
        include_noAddress = findViewById(R.id.include_choose_noAddress);//无收货地址
        include_address.setOnClickListener(this);
        include_noAddress.setOnClickListener(this);

        include_address.setVisibility(View.GONE);// TODO  暂时设为不可见

        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_phone = (TextView) findViewById(R.id.tv_phone);
        tv_address = (TextView) findViewById(R.id.tv_address);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            //返回
            case R.id.btn_main_left:
                finish();
                break;

            //有地址，前去修改
            case R.id.include_choose_address:
                intent = new Intent(this, ChooseAddressActivity.class);
                startActivityForResult(intent, CODE_ADDRESS);
                break;

            //无地址，添加收货地址
            case R.id.include_choose_noAddress:
                intent = new Intent(this, ChooseAddressActivity.class);
                startActivityForResult(intent, CODE_ADDRESS);
                break;


            case R.id.rl_appoint_time:
                MyDatePicker2 datePicker = new MyDatePicker2(this);
                datePicker.selectDateDialog(tv_appoint_time, "2016-01-26 17:26");
                break;

            case R.id.rl_call_time:
                List<String> list = new ArrayList<>();
                for (int i = 0; i < 24; i++) {
                    list.add(i + "时");
                }
                showPopPositionWindow(v, list);
                break;

            //立即预约
            case R.id.tv_now:
                square = et_square.getText().toString();
                appointTime = tv_appoint_time.getText().toString();
                callTime = tv_call_time.getText().toString();
                if (!"".equals(appointTime) && !"".equals(callTime) && !"".equals(square)) {
                    upload();
                } else {
                    MyToast("请填写完整信息");
                }
                break;

            default:
                break;

        }

    }

    private String pickCard, pickCard2;

    private void showPopPositionWindow(View view, final List<String> list) {

        // 一个自定义的布局，作为显示的内容
        View contentView = LayoutInflater.from(this).inflate(R.layout.pop_educate2, null);
        PickerView picker = (PickerView) contentView.findViewById(R.id.picker_educate);
        PickerView picker2 = (PickerView) contentView.findViewById(R.id.picker_educate2);
        TextView pickersure = (TextView) contentView.findViewById(R.id.picker_submit);
        TextView pickercancel = (TextView) contentView.findViewById(R.id.picker_cancel);
        /*final PopupWindow popupWindow = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT, true);*/
        final Dialog dialog = MyDialog.getButtonDialog(this, contentView);
        ArrayList<Object> str = new ArrayList<Object>();
        ArrayList<Object> str2 = new ArrayList<Object>();
        for (int i = 0; i < list.size(); i++) {
            str.add(list.get(i));
            str2.add(list.get(i));
        }

        picker.setData(str);
        picker2.setData(str2);
        int size = list.size();
        int defaultNum = size / 2;
        pickCard = str.get(defaultNum).toString();
        pickCard2 = str.get(defaultNum).toString();
        picker2.setOnSelectListener(new PickerView.onSelectListener() {
            @Override
            public void onSelect(String text) {
                pickCard2 = text;
            }
        });
        picker.setOnSelectListener(new PickerView.onSelectListener() {

            @Override

            public void onSelect(String text) {
                pickCard = text;
            }
        });

        pickercancel.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {
                dialog.hide();

            }
        });
        pickersure.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {
                if (null != pickCard) {
                    tv_call_time.setText(pickCard + "至" + pickCard2);
                }
                dialog.hide();
            }
        });

    }

    private static final String TAG = "AppointMentMessageActivity";

    private void showDateDialog() {
        MyDatePicker3 dataPicker = new MyDatePicker3(AppointMentMessageActivity.this);
        dataPicker.selectDateDialog(tv_call_time, "1992-11-01", "birth");
    }

    private void upload() {
        System.out.println("Time==== " + pickCard);
        System.out.println("Time==== " + pickCard2);
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("addressId", addrId);
        map.put("packageId", bean.getGoods_id());
        map.put("area", square);
        map.put("start_time", pickCard);
        map.put("end_time", pickCard2);
        map.put("phone", MyApplication.getInstance().getUserBean().getPhone());
        map.put("home_time", appointTime);
        XUtil.Post(Url.PACKAGE_MAKEAPPOINTMENT, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e(TAG, "onSuccess: " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String str = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(str)) {
                        MyToast(info);
                        finish();
                    } else {
                        MyToast(info);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            //地址回调
            case CODE_ADDRESS:
                if (resultCode == RESULT_OK) {
                    if (null != data) {
                        AddressBean addressBean = (AddressBean) data.getSerializableExtra("addressBean");
                        include_address.setVisibility(View.VISIBLE);
                        include_noAddress.setVisibility(View.GONE);
                        String province = addressBean.getProvince();
                        String city = addressBean.getCity();
                        String district = addressBean.getDistrict();
                        String place = addressBean.getPlace();
                        String addr = province + city + district + place;
                        tv_name.setText(addressBean.getConsignee());
                        tv_phone.setText(addressBean.getTelephone());
                        tv_address.setText(addr);
                        addrId = addressBean.getAddress_id();
                    }
                }
                break;

            default:
                break;
        }
    }
}
